﻿using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Cmd;
using DG.Tweening;
using GameData;
using UnityEngine.UI;

public class InputVipView : MonoBehaviour {

    public GameObject contentObj;
    public GameObject vipNumPre;
    public InputField inputField;
    public GameObject setVipBtn;
    public GameObject unVipBtn;
    public GameObject backBtn;
        public ToggleGroup ToggleGroup;


    // Use this for initialization
    void Start () {
        EventTriggerListener.Get(setVipBtn).onClick = OnSetVIP;
        EventTriggerListener.Get(unVipBtn).onClick = OnSetUnVIP;
        EventTriggerListener.Get(backBtn).onClick = OnBack;

    }

    public void OnBack(GameObject obj)
    {
        transform.localScale = new Vector3(1, 1, 1);
        Tweener tw = transform.DOScale(new Vector3(0, 0, 0), 0.3f).SetEase(Ease.OutBack);
        tw.OnComplete(()=> gameObject.SetActive(false));
        OnClose();
        

    }

    public void OnShow()
    {

        GetRecAndVIPNumSend ns = new GetRecAndVIPNumSend();
        ns.Send(GlobalVO.GetInstance().roleInfo.UserInfo, OnNumListData);

        Messenger<NumListData>.AddListenerByOne("SC取号和VIP信息", OnNumListData);
    }

    public void OnClose()
    {
        Messenger<NumListData>.RemoveListener("SC取号和VIP信息", OnNumListData);


    }


    void OnSetVIP(GameObject obj)
    {
        if (string.IsNullOrEmpty(inputField.text))
        {
            Debug.Log("VIp nonono");
            return;
        }
        /*
        SetNumToVIPSend ss = new SetNumToVIPSend();
        SetNumToVIP sn = new SetNumToVIP();
        sn.NumberInfoList = new List<NumberInfo>();
        int newid = int.Parse(inputField.text);

        NumberInfo n = new NumberInfo();
        n.value = newid;
        n.isVIP = true;
        n.state = (int)NumberState.Receive;
        sn.NumberInfoList.Add(n);
        ss.Send(sn, OnNumListData);
        */

        int count = int.Parse(inputField.text);
        CreateNumByNum cr = new CreateNumByNum();
        NumberData nd = new NumberData();
        nd.value = count;
        cr.Send(nd, OnCNNR);


    }

    void OnCNNR(NumListData numListData)
    {
        
    }

    void OnSetUnVIP(GameObject obj)
    {
        List<Toggle> pl = new List<Toggle>();
        for (int i = 0; i < contentObj.transform.childCount; i++)
        {
            Toggle to = contentObj.transform.GetChild(i).GetComponent<Toggle>();
            if (to.gameObject.activeSelf)
            {
                if (to.isOn)
                {
                    pl.Add(to);

                }
            }


        }

        Debug.Log("取消优先 数量:" + pl.Count());
        // ToggleGroup.ActiveToggles().Count();
        if (pl.Count > 0)
        {
            SetNumToVIPSend ss = new SetNumToVIPSend();
            SetNumToVIP sn = new SetNumToVIP();
            sn.NumberInfoList = new List<NumberInfo>();

            for (int i = 0; i < pl.Count; i++)
            {
                Toggle to = pl[i];
                if (to.isOn)
                {
                    NumberInfo n = pl[i].gameObject.GetComponent<VIPNum>().NumberInfo;
                    n.isVIP = false;
                    sn.NumberInfoList.Add(n);
                }

            }


            ss.Send(sn, OnNumListData);
        }
    }


    void OnNumListData(NumListData numListData)
    {
       // OnBack(null);
        return;

        for (int i = 0; i < contentObj.transform.childCount; i++)
        {
            Toggle to = contentObj.transform.GetChild(i).GetComponent<Toggle>();
            to.isOn = false;

        }

        Debug.Log("VIP数量:"+ numListData.list2.Count);
        //设置
        List<NumberInfo> newlist = new List<NumberInfo>();
        newlist.AddRange(numListData.list2);
        newlist.AddRange(numListData.list1);
        newlist.Sort(UtilTool.SortCompareByValue);
        int numCount = numListData.list2.Count;
        //
        int childCount = contentObj.transform.childCount;
        int vnck = Math.Max(childCount, numCount);

        for (int i = 0; i < vnck; i++)
        {

            if (i < numCount)
            {
                VIPNum vipnum;
                if (i < childCount)
                {
                    Transform objtf = contentObj.transform.GetChild(i);
                    objtf.gameObject.SetActive(true);
                    vipnum = objtf.GetComponent<VIPNum>();

                }
                else
                {
                    GameObject obj = GameObject.Instantiate(vipNumPre);
                    obj.transform.SetParent(contentObj.transform,false);
                    obj.transform.SetAsLastSibling();
                    vipnum = obj.GetComponent<VIPNum>();
                }
                //
                NumberInfo numberInfo = newlist[i];
                numberInfo.isVIP = UtilTool.checkNumInDic(numListData.list2, numberInfo);
                vipnum.SetNumber(numberInfo);
                //

                //text.text = "号码：" + numberInfo.value + "  排:" + numberInfo.QueueNum + "位";
            }
            else
            {
                Transform objtf = contentObj.transform.GetChild(i);
                objtf.gameObject.SetActive(false);

            }

        }

        GridLayoutGroup gridLayoutGroup = contentObj.GetComponent<GridLayoutGroup>();
        RectTransform rec = contentObj.GetComponent<RectTransform>();
        //设置高度
        int callheight = (int)Math.Ceiling(numCount / 5.0f);
        float newheight = callheight * gridLayoutGroup.cellSize.y+50;
        rec.sizeDelta = new Vector2(rec.sizeDelta.x, newheight);

    }


    // Update is called once per frame
    void Update () {
	
	}
}
